#!/usr/local/bin/Resource/www/cgi-bin/php
<?php
function str_between($string, $start, $end){
	$string = " ".$string; $ini = strpos($string,$start);
	if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini;
	return substr($string,$ini,$len);
}
$link = $_GET["file"];
$link=urldecode($link);
$html = file_get_contents($link);
if(preg_match_all("/(http\b.*?)(\"|\')+/i",$html,$matches)) {
$links=$matches[1];
//print_r ($links);
}
$s="/youtube|player\.vimeo\.com/i";
for ($i=0;$i<count($links);$i++) {
  $cur_link=$links[$i];
  if (strpos($cur_link,"player.vimeo.com") !== false) {
      $id=str_between($cur_link,"video/","?");
      break;
  } else if (strpos($cur_link,"youtube") !== false) {
     $link=file_get_contents("http://127.0.0.1/cgi-bin/scripts/util/yt.php?file=".$cur_link);
     print $link;
     break;
  }
}
if (strpos($cur_link,"player.vimeo.com") !== false) {
$l="http://www.vimeo.com/moogaloop/load/clip:".$id;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $l);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
  curl_setopt($ch,CURLOPT_REFERER,"http://creativemonkeyz.com/category/roboti/");
  $page = curl_exec($ch);
  curl_close($ch);
$sig=str_between($page,'<request_signature>','</request_signature>');
$sig1=str_between($page,'<request_signature_expires>','</request_signature_expires>');
$hd1=str_between($page,'<isHD>','</isHD>');
if ($hd1 == "1") {
  $hd="hd";
} else {
  $hd="sd";
}
$l = "http://www.vimeo.com/moogaloop/play/clip:".$id."/".$sig."/".$sig1."/?q=".$hd;
$ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $l);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_NOBODY, true);
  $page = curl_exec($ch);
  curl_close($ch);
$t1=explode("Location:",$page);
$t2=explode("Vary",$t1[2]);
$link=trim($t2[0]);
print $link;
}
?> 
